<?php
namespace Resource\Service;
use Think\Log;
use Think\Model;
/**
 * 商品表
 * Created by PhpStorm.
 * User: alex
 * Date: 2017/6/15
 * Time: 18:27
 */
class ItemService extends Model{
    protected $trueTableName = 'wz_item';

    /**
     * @param $params
     * @return array|bool
     */
    public function addItem($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $item_id = D('Resource/Item')->addItem($params);
        if($item_id) {
            return ['item_id'=>$item_id];
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getItemInfo($params, $fields='') {
        $ItemInfo = D('Resource/Item')->getItemInfo($params,$fields);
        return $ItemInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchItemList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        $data['status'] = ['in',[-1,0,1]];
        isset($params['name']) && $data['name'] = ['like', "%{$params['name']}%"];
        isset($params['is_enquiry']) && $data['is_enquiry'] =$params['is_enquiry'];
        isset($params['parent_cid']) && $data['parent_cid'] =$params['parent_cid'];
        isset($params['second_cid']) && $data['second_cid'] =$params['second_cid'];
        isset($params['three_cid']) && $data['three_cid'] =$params['three_cid'];
        isset($params['status']) && $data['status'] =$params['status'];

        //查询项目总数
        $total = D('Resource/Item')->getItemCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询项目列表
        $itemList = D('Resource/Item')->getItemList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['itemList'] = $itemList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateItemInfo($params) {
        return D('Resource/Item')->updateItemInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllItemList($params){
        $dataInfo = D('Resource/Item')->getAllItemList($params);
        return $dataInfo;
    }
}